﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Worktool.CODE_Chris.SQL_DATA;

namespace Worktool.CODE_Chris.Forms.MaterialFlow
{
    public partial class DeliveryChainFilter : Form
    {
        private SQL sql;
        private DeliveryChain parent;
        private Boolean isInit = true;

        public DeliveryChainFilter(DeliveryChain parent)
        {
            this.parent = parent;
            InitializeComponent();
            init();
        }

        private void init()
        {
            sql = new SQL();

            fillElementsInit();

            setInactiveFilters();

            isInit = false;
        }

        #region Init
        private void fillElementsInit()
        {
            try
            {
                String[,] combo1 = sql.receiveSQL("SELECT COUNT(*) AS Amount FROM (SELECT DISTINCT bestellFreigabe FROM vw_comm_Lieferkette WHERE bestellFreigabe IS NOT NULL)a", "SELECT DISTINCT bestellFreigabe FROM vw_comm_Lieferkette WHERE bestellFreigabe IS NOT NULL ORDER BY bestellFreigabe");
                fillComboBox(comboBox1Von, combo1, "Erster Termin");
                fillComboBox(comboBox1Bis, combo1, "Letzter Termin");

                String[,] combo2 = sql.receiveSQL("SELECT COUNT(*) AS Amount FROM (SELECT DISTINCT bestellung FROM vw_comm_Lieferkette WHERE bestellung IS NOT NULL)a", "SELECT DISTINCT bestellung FROM vw_comm_Lieferkette WHERE bestellung IS NOT NULL ORDER BY bestellung");
                fillComboBox(comboBox2Von, combo2, "Erster Termin");
                fillComboBox(comboBox2Bis, combo2, "Letzter Termin");

                String[,] combo3 = sql.receiveSQL("SELECT COUNT(*) AS Amount FROM (SELECT DISTINCT bestaetigterLiefertermin FROM vw_comm_Lieferkette WHERE bestaetigterLiefertermin IS NOT NULL)a", "SELECT DISTINCT bestaetigterLiefertermin FROM vw_comm_Lieferkette WHERE bestaetigterLiefertermin IS NOT NULL ORDER BY bestaetigterLiefertermin");
                fillComboBox(comboBox3Von, combo3, "Erster Termin");
                fillComboBox(comboBox3Bis, combo3, "Letzter Termin");

                String[,] combo4 = sql.receiveSQL("SELECT COUNT(*) AS Amount FROM (SELECT DISTINCT wareneingang FROM vw_comm_Lieferkette WHERE wareneingang IS NOT NULL)a", "SELECT DISTINCT wareneingang FROM vw_comm_Lieferkette WHERE wareneingang IS NOT NULL ORDER BY wareneingang");
                fillComboBox(comboBox4Von, combo4, "Erster Termin");
                fillComboBox(comboBox4Bis, combo4, "Letzter Termin");

                String[,] combo5 = sql.receiveSQL("SELECT COUNT(*) AS Amount FROM (SELECT DISTINCT wareLieferSollHH FROM vw_comm_Lieferkette WHERE wareLieferSollHH IS NOT NULL)a", "SELECT DISTINCT wareLieferSollHH FROM vw_comm_Lieferkette WHERE wareLieferSollHH IS NOT NULL ORDER BY wareLieferSollHH");
                fillComboBox(comboBox5Von, combo5, "Erster Termin");
                fillComboBox(comboBox5Bis, combo5, "Letzter Termin");

                String[,] combo6 = sql.receiveSQL("SELECT COUNT(*) AS Amount FROM (SELECT DISTINCT wareLieferIstHH FROM vw_comm_Lieferkette WHERE wareLieferIstHH IS NOT NULL)a", "SELECT DISTINCT wareLieferIstHH FROM vw_comm_Lieferkette WHERE wareLieferIstHH IS NOT NULL ORDER BY wareLieferIstHH");
                fillComboBox(comboBox6Von, combo6, "Erster Termin");
                fillComboBox(comboBox6Bis, combo6, "Letzter Termin");

                String[,] combo7 = sql.receiveSQL("SELECT COUNT(*) AS Amount FROM (SELECT DISTINCT lieferungWerftIst FROM vw_comm_Lieferkette WHERE lieferungWerftIst IS NOT NULL)a", "SELECT DISTINCT lieferungWerftIst FROM vw_comm_Lieferkette WHERE lieferungWerftIst IS NOT NULL ORDER BY lieferungWerftIst");
                fillComboBox(comboBox7Von, combo7, "Erster Termin");
                fillComboBox(comboBox7Bis, combo7, "Letzter Termin");

                String[,] combo8 = sql.receiveSQL("SELECT COUNT(*) AS Amount FROM (SELECT DISTINCT lieferungSchiffSoll FROM vw_comm_Lieferkette WHERE lieferungSchiffSoll IS NOT NULL)a", "SELECT DISTINCT lieferungSchiffSoll FROM vw_comm_Lieferkette WHERE lieferungSchiffSoll IS NOT NULL ORDER BY lieferungSchiffSoll");
                fillComboBox(comboBox8Von, combo8, "Erster Termin");
                fillComboBox(comboBox8Bis, combo8, "Letzter Termin");

            }
            catch (Exception e)
            {

            }
        }

        private void fillComboBox(ComboBox box, String[,] data, String firstElement)
        {
            DateTime dateValue;

            box.Items.Clear();
            box.Items.Add(firstElement);
            box.DropDownHeight = 500;

            for (int a = 0; a < data.GetLength(1); a++)
            {
                if(DateTime.TryParse(data[0,a], out dateValue)) box.Items.Add(dateValue.Date);
            }
            box.Refresh();
            if (firstElement == "Erster Termin") box.Text = checkParentFilterValues(box.Name, true); else box.Text = checkParentFilterValues(box.Name, false);
        }

        private String checkParentFilterValues(String name, Boolean first)
        {
            if (name == "comboBox1Von" && parent.fBestellfreigabeVon != null) if(comboBox1Von.FindString(parent.fBestellfreigabeVon) != -1){return parent.fBestellfreigabeVon;}
            if (name == "comboBox1Bis" && parent.fBestellfreigabeBis != null) if(comboBox1Bis.FindString(parent.fBestellfreigabeVon) != -1){return parent.fBestellfreigabeBis;}
            if (name == "comboBox2Von" && parent.fBestellterminVon != null) if (comboBox2Von.FindString(parent.fBestellterminVon) != -1) { return parent.fBestellterminVon; }
            if (name == "comboBox2Bis" && parent.fBestellterminBis != null) if (comboBox2Bis.FindString(parent.fBestellterminBis) != -1) { return parent.fBestellterminBis; }
            if (name == "comboBox3Von" && parent.fBestaetigterLieferterminVon != null) if (comboBox3Von.FindString(parent.fBestaetigterLieferterminVon) != -1) { return parent.fBestaetigterLieferterminVon; }
            if (name == "comboBox3Bis" && parent.fBestaetigterLieferterminBis != null) if (comboBox3Bis.FindString(parent.fBestaetigterLieferterminBis) != -1) { return parent.fBestaetigterLieferterminBis; }
            if (name == "comboBox4Von" && parent.fWareneingangVon != null) if (comboBox4Von.FindString(parent.fWareneingangVon) != -1) { return parent.fWareneingangVon; }
            if (name == "comboBox4Bis" && parent.fWareneingangBis != null) if (comboBox4Bis.FindString(parent.fWareneingangBis) != -1) { return parent.fWareneingangBis; }
            if (name == "comboBox5Von" && parent.fSollBereitstellungHHVon != null) if (comboBox5Von.FindString(parent.fSollBereitstellungHHVon) != -1) { return parent.fSollBereitstellungHHVon; }
            if (name == "comboBox5Bis" && parent.fSollBereitstellungHHBis != null) if (comboBox5Bis.FindString(parent.fSollBereitstellungHHBis) != -1) { return parent.fSollBereitstellungHHBis; }
            if (name == "comboBox6Von" && parent.fIstBereitstellungHHVon != null) if (comboBox6Von.FindString(parent.fIstBereitstellungHHVon) != -1) { return parent.fIstBereitstellungHHVon; }
            if (name == "comboBox6Bis" && parent.fIstBereitstellungHHBis != null) if (comboBox6Bis.FindString(parent.fIstBereitstellungHHBis) != -1) { return parent.fIstBereitstellungHHBis; }
            if (name == "comboBox7Von" && parent.fLieferterminAnWerftVon != null) if (comboBox7Von.FindString(parent.fLieferterminAnWerftVon) != -1) { return parent.fLieferterminAnWerftVon; }
            if (name == "comboBox7Bis" && parent.fLieferterminAnWerftBis != null) if (comboBox7Bis.FindString(parent.fLieferterminAnWerftBis) != -1) { return parent.fLieferterminAnWerftBis; }
            if (name == "comboBox8Von" && parent.fSollBereitstellungSchiffVon != null) if (comboBox8Von.FindString(parent.fSollBereitstellungSchiffVon) != -1) { return parent.fSollBereitstellungSchiffVon; }
            if (name == "comboBox8Bis" && parent.fSollBereitstellungSchiffBis != null) if (comboBox8Von.FindString(parent.fSollBereitstellungSchiffBis) != -1) { return parent.fSollBereitstellungSchiffBis; }

            if (first) return "Erster Termin"; else return "Letzter Termin";
        }

        private void setInactiveFilters()
        {
            if (!parent.f1IsActive) { setComboInactive(comboBox1Von, comboBox1Bis); checkBox1.Checked = false; }
            if (!parent.f2IsActive) { setComboInactive(comboBox2Von, comboBox2Bis); checkBox2.Checked = false; }
            if (!parent.f3IsActive) { setComboInactive(comboBox3Von, comboBox3Bis); checkBox3.Checked = false; }
            if (!parent.f4IsActive) { setComboInactive(comboBox4Von, comboBox4Bis); checkBox4.Checked = false; }
            if (!parent.f5IsActive) { setComboInactive(comboBox5Von, comboBox5Bis); checkBox5.Checked = false; }
            if (!parent.f6IsActive) { setComboInactive(comboBox6Von, comboBox6Bis); checkBox6.Checked = false; }
            if (!parent.f7IsActive) { setComboInactive(comboBox7Von, comboBox7Bis); checkBox7.Checked = false; }
            if (!parent.f8IsActive) { setComboInactive(comboBox8Von, comboBox8Bis); checkBox8.Checked = false; }
        }

        private void setComboInactive(ComboBox box1, ComboBox box2)
        {
            box1.Enabled = false;
            box2.Enabled = false;
        }
        #endregion

        #region Events
        private void checkBoxes_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox check = (CheckBox)sender;
            
            if (!isInit)
            {
                if (check.Name == "checkBox1") { comboBox1Von.Enabled = check.Checked; comboBox1Bis.Enabled = check.Checked; parent.f1IsActive = check.Checked; }
                if (check.Name == "checkBox2") { comboBox2Von.Enabled = check.Checked; comboBox2Bis.Enabled = check.Checked; parent.f2IsActive = check.Checked; }
                if (check.Name == "checkBox3") { comboBox3Von.Enabled = check.Checked; comboBox3Bis.Enabled = check.Checked; parent.f3IsActive = check.Checked; }
                if (check.Name == "checkBox4") { comboBox4Von.Enabled = check.Checked; comboBox4Bis.Enabled = check.Checked; parent.f4IsActive = check.Checked; }
                if (check.Name == "checkBox5") { comboBox5Von.Enabled = check.Checked; comboBox5Bis.Enabled = check.Checked; parent.f5IsActive = check.Checked; }
                if (check.Name == "checkBox6") { comboBox6Von.Enabled = check.Checked; comboBox6Bis.Enabled = check.Checked; parent.f6IsActive = check.Checked; }
                if (check.Name == "checkBox7") { comboBox7Von.Enabled = check.Checked; comboBox7Bis.Enabled = check.Checked; parent.f7IsActive = check.Checked; }
                if (check.Name == "checkBox8") { comboBox8Von.Enabled = check.Checked; comboBox8Bis.Enabled = check.Checked; parent.f8IsActive = check.Checked; }
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            //Datumscheck ("Bis" darf nicht größer als "Von" sein)
            if (checkBox1.Checked && !checkDates(comboBox1Von, comboBox1Bis)) { showErrorMessage("Bestellfreigabe"); return; }
            if (checkBox2.Checked && !checkDates(comboBox2Von, comboBox2Bis)) { showErrorMessage("Bestelltermin"); return; }
            if (checkBox3.Checked && !checkDates(comboBox3Von, comboBox3Bis)) { showErrorMessage("Bestätigter Liefertermin"); return; }
            if (checkBox4.Checked && !checkDates(comboBox4Von, comboBox4Bis)) { showErrorMessage("Wareneingang ASC"); return; }
            if (checkBox5.Checked && !checkDates(comboBox5Von, comboBox5Bis)) { showErrorMessage("Soll Bereitstellung HH"); return; }
            if (checkBox6.Checked && !checkDates(comboBox6Von, comboBox6Bis)) { showErrorMessage("Ist Bereitstellung HH"); return; }
            if (checkBox7.Checked && !checkDates(comboBox7Von, comboBox7Bis)) { showErrorMessage("Liefertermin an Werft"); return; }
            if (checkBox8.Checked && !checkDates(comboBox8Von, comboBox8Bis)) { showErrorMessage("Soll Bereitstellung Schiff"); return; }

            //Datumswerte schreiben
            setNewValues();

            //Filter setzen
            parent.setMainFilters();

            this.Close();
        }

        private Boolean checkDates(ComboBox box1, ComboBox box2)
        {

            if (box1.Text != "Erster Termin" && box2.Text != "Letzter Termin")
            {
                //Beide Datumsangaben wurden gemacht & Bis-Datum ist kleiner als Von-Datum -> Fehler
                if (DateTime.Parse(box2.Text) < DateTime.Parse(box1.Text)) return false;
            }

            return true;
        }

        private void showErrorMessage(String bereich)
        {
            MessageBox.Show("Die Datumsangabe für den Bereich \"" + bereich + "\" ist nicht korrekt. Das Bis-Datum darf nicht kleiner als das Von-Datum sein. Bitte die Werte korrigieren.", "Fehler");
        }

        private void setNewValues()
        {
            if (checkBox1.Checked && comboBox1Von.Text != "Erster Termin") parent.fBestellfreigabeVon = comboBox1Von.Text;
            if (checkBox1.Checked && comboBox1Bis.Text != "Letzter Termin") parent.fBestellfreigabeBis = comboBox1Bis.Text;
            if (checkBox2.Checked && comboBox2Von.Text != "Erster Termin") parent.fBestellterminVon = comboBox2Von.Text;
            if (checkBox2.Checked && comboBox2Bis.Text != "Letzter Termin") parent.fBestellterminBis = comboBox2Bis.Text;
            if (checkBox3.Checked && comboBox3Von.Text != "Erster Termin") parent.fBestaetigterLieferterminVon = comboBox3Von.Text;
            if (checkBox3.Checked && comboBox3Bis.Text != "Letzter Termin") parent.fBestaetigterLieferterminBis = comboBox3Bis.Text;
            if (checkBox4.Checked && comboBox4Von.Text != "Erster Termin") parent.fWareneingangVon = comboBox4Von.Text;
            if (checkBox4.Checked && comboBox4Bis.Text != "Letzter Termin") parent.fWareneingangBis = comboBox4Bis.Text;
            if (checkBox5.Checked && comboBox5Von.Text != "Erster Termin") parent.fSollBereitstellungHHVon = comboBox5Von.Text;
            if (checkBox5.Checked && comboBox5Bis.Text != "Letzter Termin") parent.fSollBereitstellungHHBis = comboBox5Bis.Text;
            if (checkBox6.Checked && comboBox6Von.Text != "Erster Termin") parent.fIstBereitstellungHHVon = comboBox6Von.Text;
            if (checkBox6.Checked && comboBox6Bis.Text != "Letzter Termin") parent.fIstBereitstellungHHBis = comboBox6Bis.Text;
            if (checkBox7.Checked && comboBox7Von.Text != "Erster Termin") parent.fLieferterminAnWerftVon = comboBox7Von.Text;
            if (checkBox7.Checked && comboBox7Bis.Text != "Letzter Termin") parent.fLieferterminAnWerftBis = comboBox7Bis.Text;
            if (checkBox8.Checked && comboBox8Von.Text != "Erster Termin") parent.fSollBereitstellungSchiffVon = comboBox8Von.Text;
            if (checkBox8.Checked && comboBox8Bis.Text != "Letzter Termin") parent.fSollBereitstellungSchiffBis = comboBox8Bis.Text;

            if (checkBox1.Checked && comboBox1Von.Text == "Erster Termin") parent.fBestellfreigabeVon = null;
            if (checkBox1.Checked && comboBox1Bis.Text == "Letzter Termin") parent.fBestellfreigabeBis = null;
            if (checkBox2.Checked && comboBox2Von.Text == "Erster Termin") parent.fBestellterminVon = null;
            if (checkBox2.Checked && comboBox2Bis.Text == "Letzter Termin") parent.fBestellterminBis = null;
            if (checkBox3.Checked && comboBox3Von.Text == "Erster Termin") parent.fBestaetigterLieferterminVon = null;
            if (checkBox3.Checked && comboBox3Bis.Text == "Letzter Termin") parent.fBestaetigterLieferterminBis = null;
            if (checkBox4.Checked && comboBox4Von.Text == "Erster Termin") parent.fWareneingangVon = null;
            if (checkBox4.Checked && comboBox4Bis.Text == "Letzter Termin") parent.fWareneingangBis = null;
            if (checkBox5.Checked && comboBox5Von.Text == "Erster Termin") parent.fSollBereitstellungHHVon = null;
            if (checkBox5.Checked && comboBox5Bis.Text == "Letzter Termin") parent.fSollBereitstellungHHBis = null;
            if (checkBox6.Checked && comboBox6Von.Text == "Erster Termin") parent.fIstBereitstellungHHVon = null;
            if (checkBox6.Checked && comboBox6Bis.Text == "Letzter Termin") parent.fIstBereitstellungHHBis = null;
            if (checkBox7.Checked && comboBox7Von.Text == "Erster Termin") parent.fLieferterminAnWerftVon = null;
            if (checkBox7.Checked && comboBox7Bis.Text == "Letzter Termin") parent.fLieferterminAnWerftBis = null;
            if (checkBox8.Checked && comboBox8Von.Text == "Erster Termin") parent.fSollBereitstellungSchiffVon = null;
            if (checkBox8.Checked && comboBox8Bis.Text == "Letzter Termin") parent.fSollBereitstellungSchiffBis = null;
        }

        private void buttonAbort_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

    }
}
